<?php
class DelCausa extends Controller
{
	function DelCausa()
	{
		parent::Controller();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('causas_model');
		$this->load->helper('text');
	}
	
	function index()
	{
		$this->load->view('mod_causas/indexDelCausa');
	}
	function validSearch()
    {
        $config = array(
               array(
                     'field'   => 'idCausaDelete',
                     'label'   => 'Rol',
                     'rules'   => 'required'
                  ));
	    $this->form_validation->set_rules($config);

        
        if($this->form_validation->run() == FALSE)
        {
           echo validation_errors('<div class="msj_error" align="center">','</div>');
        	
        }
        else
        {
        	$this->showCausaDelete();
        } 
    }
	/*
	 * Muestra la vista showCausasAdmin,antes de eso se verifica en la base de datos,
	 * la existencia de los datos.
	 */
	
	function showCausaDelete()
	{
		$causaDel = $this->input->post('idCausaDelete');
		$data['results'] = $this->causas_model->get_delete_edit_like(1000,$causaDel);
		
		if($data['results'] ->num_rows() != 0)
		{
		    $this->load->view('mod_causas/delCausa',$data);  	
			
		}
		else
		{
			echo "<div class='msj_error' align='center'>No existen coincidencias para la causa solicitada.</div>";
		}
		
	
	}
	
	/*
	 * Con esta funcion eliminaremos la causa de la base de datos.
	 */
	function deleteCausa()
	{
		$this->load->helper('file');
		$idCausa = $this->uri->segment(4);
		$files = $this->causas_model->getFilesCausa($idCausa);
		if($this->causas_model->delCausa($idCausa)){
			//delete_files('causas/'.$idCausa);	
			foreach($files->result() as $file):
				//$this->firephp->log($file->ruta_archivo,'$file');
				$old = umask(0);
				chmod($file->ruta_archivo,0777);
				unlink($file->ruta_archivo);
				umask($old);
			endforeach;
			echo "<div class = 'msj_success' align='center'> Operacion Realizada con exito.</div>";

		}
		else
			echo "<div class = 'msj_success' align='center'> Hubo problemas al eliminar la causa, intentelo nuevamente.</div>";
			//$this->load->view('mod_causas/Success');
	}
	/*
	 * Esta funcion trae desde la base de datos el rol para la autocompletacion.
	 */
	function getRol()
	{
		$opc = $this->uri->segment(4);
		switch($opc)
		{
			case 1:
				$rolid = $this->input->post('idCausa');
				$rol = $this->causas_model->getRolLike(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike(1000,$rolid,true);
			break;
			
			case 2:
				$rolid = $this->input->post('rolUpfile');
				$rol = $this->causas_model->getRolLike_all(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_all(1000,$rolid,true);
			break;
			
			case 3:
				$rolid = $this->input->post('idCausaEdit');
				$rol = $this->causas_model->getRolLike_all(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_all(1000,$rolid,true);
			break;
			
			case 4:
				$rolid = $this->input->post('idCausaDelete');
				$rol = $this->causas_model->getRolLike_all(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_all(1000,$rolid,true);
			break;
			case 5:
				$rolid = $this->input->post('rol_tabla');
				$rol = $this->causas_model->getRolLike_all(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_all(1000,$rolid,true);
			break;
			case 6:
				$rolid = $this->input->post('rol_tabla_edit');
				$rol = $this->causas_model->getRolLike_all(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_all(1000,$rolid,true);
			break;
			case 7:
				$rolid = $this->input->post('idCausa_histo');
				$rol = $this->causas_model->getRolLike_histo(1000,$rolid,true);
				$materia = $this->causas_model->getMateriaLike_histo(1000,$rolid,true);
			break;
					
		}

		
		echo "<ul>";
		if($rol->num_rows() != 0){
			foreach ($rol->result() as $row)
			{
					echo "<li>".$row->rol."</li>";
				
			}
		}
		else{
			foreach ($materia->result() as $row)
			{
				echo "<li>".character_limiter($row->materia,13)."</li>";
			}
		}
		echo "</ul>";	
	}
}